package com.qf.work;

import com.qf.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author 郗昊天
 * @version 1.0
 * @package com.qf.simple
 * @date 2022/10/14 20:53
 */
public class Consumer {
    private static final String QUEUENAME="xht";
    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtils.getconnection();
        final Channel channel = connection.createChannel();
        //消费者启动的时候生产者还没有启动 所以要声明队列
        channel.queueDeclare(QUEUENAME,false,false,false,null);
        //每次只给一条消息  处理完后给下一条
        channel.basicQos(1);
        //参数1  从哪个队列中收消息
        //2  是否自动应答
        channel.basicConsume(QUEUENAME, false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String msg = new String(body);
                System.out.println("消费者收到的消息是11" + msg);
                //应答服务器  参数2  是否批量应答 false只应答当前消息
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });



    }
}
